
import { _decorator, Component, Node, UIOpacity, EventTouch, Vec2, Vec3, UITransform, v3, CameraComponent, Game, CCInteger } from 'cc';
import { EventMgr } from '../../../Framework/Scripts/Managers/EventMgr';
import { constant } from '../constant';

import { GameApp } from '../GameApp';
const { ccclass, property } = _decorator;



@ccclass('joyStick')
export class joyStick extends Component {



    @property({ type: CCInteger, displayName: "半径" })
    maxRadius = 100;


    @property({ type: Node, displayName: "触点范围" })
    touchArea: Node | null = null;//触点范围

    @property({ type: Node, displayName: "按钮组件" })
    control: Node | null = null;//按钮组件

    @property({ type: UIOpacity, displayName: "箭头" })//箭头
    indexArrow: UIOpacity = null!;

    @property({ type: Node, displayName: "整个摇杆" })//整个摇杆
    joyStick: Node = null!;

    public posVec3: Vec3 = new Vec3()//方向
    public degree: number = 0//角度
    private isMove: boolean = false
    public radians: number = 0
    //  private vector: Vec2 = new Vec2()
    private cameraUI: CameraComponent = null;
    // private camera3D: CameraComponent = null;
    private oldPos: Vec3 = new Vec3()


    onLoad() {
        this.cameraUI = GameApp.Instance.CanvasCamera
        // this.camera3D = GameApp.Instance.Camera.node.getComponent(CameraComponent)

        this.oldPos = this.joyStick.getPosition()
        this.indexArrow.opacity = 0
      this.joyStick.getComponent(UIOpacity).opacity=0
        //this.joyStick.active=false




    }
    onEnable() {
        this.touchArea.on(Node.EventType.TOUCH_START, this.touchStart, this);
        // this.touchArea.on(Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchArea.on(Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchArea.on(Node.EventType.TOUCH_END, this.touchEnd, this);
        this.touchArea.on(Node.EventType.TOUCH_CANCEL, this.touchEnd, this);

    }

    onDisable() {

        this.touchArea.off(Node.EventType.TOUCH_START, this.touchStart, this);
        //  this.touchArea.off(Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchArea.off(Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchArea.off(Node.EventType.TOUCH_END, this.touchEnd, this);
        this.touchArea.off(Node.EventType.TOUCH_CANCEL, this.touchEnd, this);

    }


    touchStart(event: EventTouch) {
       // this.joyStick.active=true
     this.joyStick.getComponent(UIOpacity).opacity=255
        let point = new Vec2(event.getLocationX(), event.getLocationY());
        let world_point = this.cameraUI.screenToWorld(new Vec3(point.x, point.y));
        let pos = this.joyStick.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(world_point.x, world_point.y));
        this.joyStick.setPosition(pos.x, pos.y);


    }
    touchMove(event: EventTouch) {
      //  this.joyStick.getComponent(UIOpacity).opacity=255
        this.isMove = true
        // 获取触点的位置，屏幕坐标
        let point = new Vec2(event.getLocationX(), event.getLocationY());
        //测试
        // console.log("UILocation", event.getUILocation())
        // console.log("locatin:", event.getLocation());
        // console.log("camera3D:", this.camera3D.screenToWorld(new Vec3(point.x, point.y)));
        // console.log("cameraUI:", this.cameraUI.screenToWorld(new Vec3(point.x, point.y)));
        // 屏幕坐标转为世界坐标
        let world_point = this.cameraUI.screenToWorld(new Vec3(point.x, point.y));
        let pos = this.joyStick.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(world_point.x, world_point.y));
        // this.joyStick.setPosition(pos.x, pos.y);
        // 如果触点长度小于我们规定好的最大半径
        if (pos.length() < this.maxRadius) {
            // 摇杆的坐标为触点坐标
            this.control.setPosition(pos.x, pos.y);
        } else {// 如果不
            // 将向量归一化
            let pos_ = pos.normalize();
            // 归一化的坐标 * 最大半径
            let x = pos_.x * this.maxRadius;
            let y = pos_.y * this.maxRadius;

            // 赋值给摇杆
            this.control.setPosition(x, y);
        }
        this.radians = Math.atan2(pos.y, pos.x)
        this.degree = this.radians / Math.PI * 180
        //   this.vector = new Vec2(this.control.position.x, this.control.position.y);
        //  this.vector.normalize()
        this.indexArrow.opacity = 255
        this.indexArrow.node.setRotationFromEuler(new Vec3(0, 0, this.degree))
        //  console.log("摇杆角度：",this.degree);

        EventMgr.Instance.Emit(constant.EVENT_NAME.isMove, { degree: this.degree, isMove: this.isMove })


    }



    touchEnd() {
     this.joyStick.getComponent(UIOpacity).opacity=0
      
        this.isMove = false
        this.joyStick.setPosition(this.oldPos)
        this.control.setPosition(Vec3.ZERO)
        this.posVec3.set(Vec3.ZERO)
        this.indexArrow.opacity = 0


        EventMgr.Instance.Emit(constant.EVENT_NAME.isMove, { degree: this.degree, isMove: this.isMove })

    }

    update(dt: number) {


        // if (this.isMove) {
        //     const r: number = util.lerp(this.degree+90,this.indexArrow.node.eulerAngles.z )
        //     this.indexArrow.node.setRotationFromEuler(new Vec3(0, 0, r))
        // }
    }
}

